<?php
class FormTable {
	private $id = null;
	private $formArray = null;
	private $maxColumns = null;

	/************************************************************************
	 ************************** PUBLIC FUNCTIONS ****************************
	 ************************************************************************/
	
	/**
	 * initializes variables
	 * @param 		string 		$id
	 */
	public function __construct($id = null){
		$this->id = $id;
		$this->formArray = array();
		$this->maxColumns = 0;
	}
	
	
	/**
	 * adds an entry for given row and given column to the array
	 * @param 		int		 	$rowPos
	 * @param 		int		 	$colPos
	 * @param 		string		$html
	 */
	public function addEntry($rowPos, $colPos, $html){
		$this->formArray[$rowPos][$colPos] = $html;
		$this->maxColumns = ($colPos > $this->maxColumns) ? $colPos : $this->maxColumns;
	}
	
	
	/**
	 * creates the form table
	 * @return 		string
	 */
	public function create(){
		$this->sort();
		
		$output = '<table class="formTable formStyle" ';
		$output .= (isset($this->id)) ? 'id="' . $this->id . '"' : '';
		$output .= '>';

		
		for ($row = 0; $row < count($this->formArray); $row++){
 			$output .= '<tr>';
			for($col = 0; $col <= $this->maxColumns; $col++){
				if (!array_key_exists($col, $this->formArray[$row]))
					$output .= '<td>&nbsp;</td>';
				else
					$output .= '<td>' . $this->formArray[$row][$col] . '</td>';
			}
 			$output .= '</tr>';
		}
		$output .= '</table>';
		return $output;
	}
	
	
	/**
	 * returns the created table
	 * @return 		string
	 */
	public function __toString(){
		return $this->create();
	}

	/************************************************************************
	 **************************** GETTER / SETTER ***************************
	 ************************************************************************/
	
	public function setId($id){
		$this->id = $id;
	}
	
	public function getId(){
		return $id;
	}

	/************************************************************************
	 ************************** PRIVATE FUNCTIONS ***************************
	 ************************************************************************/
	
	/**
	 * sorts the array 
	 */
	private function sort(){
		foreach($this->formArray as &$rowPos)
			ksort($rowPos,SORT_NUMERIC);
		
		ksort($this->formArray, SORT_NUMERIC);
	}
}

?>